#include "FilterText.hpp"
#include "Index.hpp"
#include "./include/http.h"

int main()
{
    // // 1. 对html文件进行解析
    // filtertext_ns::FilterText ft("./include/boost/boost_1_89_0/doc/html/", "./data/raw_data");
    // ft.Fl_Text();

    // 2. 建立前序索引和逆序索引
    index_ns::Index search("./data/raw_data");
    search.Build_Index();

    // 3. 启动服务器
    httplib::Server svr;
    svr.set_mount_point("/", "./web_root");
    svr.Get("/search", [&](const httplib::Request &req, httplib::Response &res)
            { 
                res.set_header("Access-Control-Allow-Origin", "*");
                    if(!req.has_param("word"))
                    {
                        res.set_content("无关键字" , "text/plain; charset=utf-8");
                        return ;
                    }
                    std::string query = req.get_param_value("word");
                    std::string answer = search.Search_String(query);
                    res.set_content(answer , "application/json"); });

    Log_ns::Log(Log_ns::Info) << "Server started";
    svr.listen("0.0.0.0", 8080);
    return 0;
}